package Leetcode;

import NodeList.ListNode;

public class detectCycle {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast){
                ListNode index = head;
                ListNode index2 = fast;
                while (index != index2){
                    index = index.next;
                    index2 = index2.next;
                }
                return index;
            }
        }
        return null;
    }
}
